package com.bf.electroplating.job;

import com.bf.electroplating.pojo.entity.CompanyDevice;
import com.bf.electroplating.service.ICompanyDataSyncService;
import com.bf.electroplating.service.ICompanyDeviceService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 企业数据同步定时任务
 * 
 * @author gzd
 * @since 2025-09-22
 */
@Slf4j
@Component
public class CompanyDataSyncJob {

    @Autowired
    private ICompanyDataSyncService companyDataSyncService;

    @Resource
    private ICompanyDeviceService companyDeviceService;
    /**
     * 每小时同步一次企业数据
     * 定时任务：每小时的第5分钟执行
     */
    @Scheduled(cron = "0 5 * * * ?")
    public void syncCompanyDataHourly() {
        try {
            log.info("开始执行企业数据同步定时任务");
            companyDataSyncService.syncAllCompanyData();
            log.info("企业数据同步定时任务执行完成");
        } catch (Exception e) {
            log.error("企业数据同步定时任务执行异常", e);
        }
    }
//    @Scheduled(cron = "0 * * * * ?")
//    public void setCompanyDataSyncService() {
//        try {
//            log.info("开始执行企业数据同步定时任务");
//            List<CompanyDevice> deviceList = companyDeviceService.list();
//            String currentDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//            for (CompanyDevice device : deviceList) {
//                companyDataSyncService.syncCompanyDeviceStatus(device.getId(),currentDate);
//            }
//            log.info("企业数据同步定时任务执行完成");
//        } catch (Exception e) {
//            log.error("企业数据同步定时任务执行异常", e);
//        }
//    }


}
